import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ride_rex/constant.dart';
import 'package:ride_rex/ui/common_banner.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../route.dart';
import 'splash_logic.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  final GuideLogic logic = GuideLogic();

  @override
  void initState() {
    firstInstall();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      child: Column(
        children: [
          banner(), //中间引导图片
        ],
      ),
    );
  }

  Widget banner() {
    return CommonBanner(
      bg: Colors.white,
      itemBuilder: (context, i) {
        if (i == logic.getCount() - 1) {
          return ButtonWidget(
            onTap: () => CommonRoute.openLogin(),
            hasInkWell: false,
            child: Image.asset(logic.getImg(i), fit: BoxFit.cover),
          );
        }

        return Image.asset(logic.getImg(i), fit: BoxFit.cover);
      },
      controller: CommonBannerController(logic.getCount(), autoStart: true),
      itemHeight: ScreenUtil().screenHeight,
      onPageChanged: (index) {
        Future.delayed(const Duration(milliseconds: 1500), () {
          if (index == logic.getCount() - 1) {
            CommonRoute.openLogin();
          }
        });
      },
    );
  }

  void firstInstall() {
    CommonStorage().setBool(StorageKey.spKeyFirstInstall, false);
  }
}
